package com.qf.springboot230203.entity;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OnePiece {
    private Integer id;
    private String name;
    private String password;
    private String sex;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date birthday;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date registTime;
    private String photopath;

    public OnePiece(String name, String password, String sex, Date birthday, Date registTime, String photopath) {
        this.name = name;
        this.password = password;
        this.sex = sex;
        this.birthday = birthday;
        this.registTime = registTime;
        this.photopath = photopath;
    }
    /**
     * 将Java对象转换成csv字符串
     * @return
     */
    public String toCSVString(){
        return this.id+","+this.name+","+this.password+","+this.sex+","+this.birthday+","+
                this.registTime+","+this.photopath;
    }

    public static OnePiece csvToBook(String csv){

        final String[] split = csv.split(",");

        final OnePiece onePiece = new OnePiece();

        onePiece.setId(Integer.valueOf(split[0]));
        onePiece.setName(split[1]);
        onePiece.setPassword(split[2]);
        onePiece.setSex(split[3]);
        onePiece.setBirthday(utils(split[4]));
        onePiece.setRegistTime(utils(split[5]));
        onePiece.setPhotopath(split[6]);
        return onePiece;

    }

    public static Date utils(String string){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=dateFormat.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
